public class Day10 {
    // 从链表中删去总和值为零的连续节点
    public ListNode removeZeroSumSublists(ListNode head) {
        ListNode head2 = new ListNode(0);
        ListNode i = head2;
        head2.next = head;
        while(i != null) {
            ListNode j = i.next;
            int sum = 0;
            while(j != null) {
                sum += j.val;
                if(sum == 0) {
                    i.next = j.next;
                }
                j = j.next;
            }
            i = i.next;
        }
        return head2.next;
    }

    // 两数相加
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // 得出较小的长度
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        int shortLen = 0;
        while(cur1 != null && cur2 != null) {
            cur1 = cur1.next;
            cur2 = cur2.next;
            shortLen++;
        }
        ListNode ret = null;
        if(cur1 != null) {
            ret = l1;
        } else {
            ret = l2;
        }
        // ret 是比较长的那个链表
        ListNode cur = ret;
        int next = 0;// 下一个进位的值
        ListNode prev = null;
        int sum = 0;
        while(shortLen-- != 0) {
            sum = l1.val + l2.val;
            cur.val = (sum + next) % 10;
            next = (sum + next) / 10;
            prev = cur;
            cur = cur.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        // 此时较短的链表已经遍历完
        while(cur != null && next != 0) {
            sum = cur.val + next;
            cur.val = sum % 10;
            next = sum / 10;
            prev = cur;
            cur = cur.next;
        }
        if(next != 0) {
            // 说明 长链表已经遍历完但是还有进制
            prev.next = new ListNode(next);
        }
        return ret;
    }
}
